package com.yl.auth.controller;


import cn.hutool.core.util.StrUtil;
import com.yl.auth.entity.Users;
import com.yl.auth.model.vo.UserVo;
import com.yl.auth.service.UsersService;
import com.yl.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author W
 * @since 2023-07-24
 */
@RestController
@RequestMapping("/admin/auth/users")
@Api(tags = "登录功能")
public class UsersController {

    @Resource
    private UsersService usersService;

    @PostMapping("/login")
    @ApiOperation(value = "登录功能")
    public Result login(@ApiParam("用户") UserVo userVo){
        String username = userVo.getUsername();
        String password = userVo.getPassword();
        Users users = new Users();
        users.setUsername(username);
        users.setPassword(password);
        if(StrUtil.isBlank(username)||StrUtil.isBlank(password)) {
            return Result.fail();
        }
        Users user1 =usersService.login(users);
        String type = user1.getType();
        System.out.println("----------------------------------------------["+type);
        if(type.equals("1")) {
            Users primId = usersService.getPrimId(user1.getId());
            return Result.ok(primId);
        }else if(type.equals("2")){
            Users jiGouDuan = usersService.getJiGouDuan(user1.getId());
            return Result.ok(jiGouDuan);
        }else if(type.equals("3")){
            Users yuanGong = usersService.getYuanGong(user1.getId());
            return Result.ok(yuanGong);
        }else {
            return Result.fail();
        }

    }

    @GetMapping("/getPrimId/{id}")
    @ApiOperation(value = "根据用户id获取当前用户的权限")
    public Result getPrimId(@PathVariable Integer id) {
        Users user = usersService.getPrimId(id);
        return Result.ok(user);
    }

    @GetMapping("/getZxPrimId/{id}")
    @ApiOperation(value = "根据id回显机构中心端的账号权限")
    public Result getZxPrimId(@PathVariable Integer id) {
        Users users = usersService.getZxPrimId(id);
        return Result.ok(users);
    }

    @GetMapping("/getJiGouDuan/{id}")
    @ApiOperation(value = "根据id回显机构端账号设置")
    public Result getJiGouDuan(@PathVariable Integer id) {
        Users users = usersService.getJiGouDuan(id);
        return Result.ok(users);
    }

    @GetMapping("/getYuanGong/{id}")
    @ApiOperation(value = "员工登录根据id回显机构端账号设置")
    public Result getYuanGong(@PathVariable Integer id) {
        Users users = usersService.getYuanGong(id);
        return Result.ok(users);
    }
}

